/**
 * Copyright (C), 2015-2021, XXX有限公司
 * FileName: XjfReportController
 * Author:   Administrator
 * Date:     2021/8/12 0012 11:56
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package cn.hxlc.tie.demo.modular.jf.controller;

import com.alibaba.fastjson.JSONArray;
import cn.hxlc.tie.demo.core.enums.WorkOrderEnum;

import cn.hxlc.tie.demo.modular.jf.model.params.XjfReportParam;

import cn.hxlc.tie.demo.modular.jf.model.result.XjfReportResult;
import cn.hxlc.tie.demo.modular.jf.model.result.XjfReqMaterialResult;

import cn.hxlc.tie.demo.modular.jf.service.XjfReportService;
import cn.hxlc.tie.libs.scanner.annotation.GetResource;
import cn.hxlc.tie.libs.scanner.annotation.PostResource;
import cn.hxlc.tie.libs.scanner.stereotype.ApiResource;
import cn.hxlc.tie.model.page.PageResult;
import cn.hxlc.tie.model.web.response.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static cn.hxlc.tie.demo.modular.jf.controller.XjfWorkOrderController.getCurrentUserid;
import static cn.hxlc.tie.model.web.response.ResponseData.DEFAULT_SUCCESS_CODE;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2021/8/12 0012
 * @since 1.0.0
 */
@RestController
@ApiResource(name = "生产汇报_jf", path = "/xuReport")
public class XjfReportController {
    @Autowired
    private XjfReportService xjfReportService;

    @GetResource(name = "分页查询列表", path = "/queryListPage", requiredLogin=true, requiredPermission = false)
    @ApiOperation(value = "分页查询列表", response = XjfReportResult.class)
    public ResponseData queryListPage(XjfReportParam param) {
        PageResult<XjfReportResult> pageBySpec = xjfReportService.findPageBySpec(param);
        return ResponseData.success(pageBySpec);
    }

    @PostResource(name = "提交汇报单", path = "/submitReportDialog")
    @ApiOperation("提交汇报单")
    public ResponseData submitReportDialog(@RequestBody XjfReportParam param) {

        param.setFcreatorid(getCurrentUserid());
        param.setFuserid(getCurrentUserid());
        xjfReportService.add(param);
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"创建成功！",null);
    }

    @PostResource(name = "批量操作", path = "/batchChangeStatus" ,requiredPermission = false)
    @ApiOperation(value = "批量禁用启用" )
    public ResponseData batchChangeState(@RequestBody Map params) {
        JSONArray jfids = (JSONArray)params.get("fid");
        Object []fid =jfids.toArray();
        Integer fstatus = (Integer)params.get("fstatus");
        xjfReportService.batchChangeState(fid, WorkOrderEnum.toEnum(fstatus));
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"操作成功",null);
    }

    @GetResource(name = "查询详情", path = "/queryDetail", requiredLogin=true, requiredPermission = false)
    @ApiOperation(value = "查询详情", response = XjfReqMaterialResult.class)
    public ResponseData queryDetail(@RequestParam Long fid) {

        return ResponseData.success(xjfReportService.detail(fid));

    }

    @PostResource(name = "提交领料单", path = "/add")
    @ApiOperation("提交领料单")
    public ResponseData add(@RequestBody XjfReportParam param) {
        param.setFcreatorid(getCurrentUserid());
        param.setFuserid(getCurrentUserid());
        xjfReportService.add(param);
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"新增成功！",null);
    }

    @PostResource(name = "修改", path = "/update")
    @ApiOperation("修改")
    public ResponseData update(@RequestBody XjfReportParam param) {
        xjfReportService.update(param);
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"修改成功！",null);
    }
    @PostResource(name = "批量删除", path = "/remove")
    @ApiOperation("批量删除")
    public ResponseData remove(@RequestBody Map params) {

        JSONArray jfids = (JSONArray)params.get("fid");
        Object [] fid =jfids.toArray();
        xjfReportService.batchDelete(fid);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"删除成功！",null);
    }
}
